<?php
// 邮件发送配置
// function wp_smtp($phpmailer){
//     $phpmailer->Mailer = "smtp";
//     $phpmailer->From = 'service@fangfajr.com';
//     $phpmailer->FromName = '方法金融';
//     $phpmailer->Sender = $phpmailer->From; //Return-Path
//     $phpmailer->AddReplyTo($phpmailer->From,$phpmailer->FromName); //Reply-To
//     $phpmailer->Host = 'smtp.exmail.qq.com';
//     $phpmailer->SMTPSecure = 'ssl';
//     $phpmailer->Port = '465';
//     $phpmailer->SMTPAuth = TRUE;
//     $phpmailer->Username = 'service@fangfajr.com';
//     $phpmailer->Password = 'why4myQQ';
// }
// add_action('phpmailer_init','wp_smtp');

// Custom WordPress Admin CSS
function admin_css() {
    echo '<link rel="stylesheet" href="'. get_theme_file_uri( '/assets/css/admin.css?v=180326' ) .'" />
    <script src="'. get_theme_file_uri( '/assets/js/admin.js?v=180409' ) .'"></script>';
}
add_action('admin_head', 'admin_css');
add_action('login_head', 'admin_css');

// REMOVE WP EMOJI
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');

// 禁用 REST API
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');

// 移除 wp-json 链接
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );

function get_ssl_avatar($avatar) {
   $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','<img src="https://secure.gravatar.com/avatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">',$avatar);
   return $avatar;
}
add_filter('get_avatar', 'get_ssl_avatar');

// Remove Open Sans that WP adds from frontend
if (!function_exists('remove_wp_open_sans')) :
    function remove_wp_open_sans() {
        wp_deregister_style( 'open-sans' );
        wp_register_style( 'open-sans', false );
    }

    // 前台删除Google字体CSS
    // add_action('wp_enqueue_scripts', 'remove_wp_open_sans');
    // 后台删除Google字体CSS
    add_action('admin_enqueue_scripts', 'remove_wp_open_sans');
endif;

// 修改 jQuery 地址
function add_scripts() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', '//cdn.bootcss.com/jquery/1.12.3/jquery.min.js', array(), '');
    wp_enqueue_script( 'jquery' );
}
if ( ! is_admin() ) :
    add_action('wp_enqueue_scripts', 'add_scripts');
endif;

// 重命名上传的中文文件名
function ft_upload_prefilter($file){
    if( preg_match("/[\x7f-\xff]/", $file['name']) ){
        date_default_timezone_set('Asia/Shanghai');
        $time = date('ymdHis', time());
        $file['name'] = $time .''. mt_rand(1,100) .'.'. pathinfo($file['name'] , PATHINFO_EXTENSION);
    }
    return $file;
}
add_filter('wp_handle_upload_prefilter', 'ft_upload_prefilter');

/* 给 编辑 增加模板相关配置权限 */
// function add_theme_caps() {
//     $editor = get_role( 'editor' );
//     $editor->add_cap( 'edit_theme_options' );
// }
// add_action( 'admin_init', 'add_theme_caps');

// 启用友情链接
// add_filter( 'pre_option_link_manager_enabled', '__return_true' );

add_filter( 'get_the_archive_title', function ( $title ) {
    if( is_category() ) {
        $title = single_cat_title( '', false );
    }
    return $title;
});

// function add_theme_caps() {
//     $role = get_role( 'editor' );
//     $role->add_cap( 'edit_theme_options' );
// }
// add_action( 'admin_init', 'add_theme_caps');

// 使小工具标题支持 HTML
// add_filter( 'widget_title', 'html_widget_title_replace' );

// function html_widget_title_replace($html_widget_title) {
//     $html_widget_title_tagopen = '['; //Our HTML opening tag replacement
//     $html_widget_title_tagclose = ']'; //Our HTML closing tag replacement

//     $html_widget_title = str_replace($html_widget_title_tagopen, '<', $html_widget_title);
//     $html_widget_title = str_replace($html_widget_title_tagclose, '>', $html_widget_title);

//     return $html_widget_title;
// }

function wpb_imagelink_setup() {
    $image_set = get_option( 'image_default_link_type' );
    if ($image_set !== 'none') {
        update_option('image_default_link_type', 'none');
        update_option('image_default_size', 'full');
    }
}
add_action('admin_init', 'wpb_imagelink_setup', 10);

// 相册默认属性值
function theme_gallery_defaults( $settings ) {
    $settings['galleryDefaults']['link'] = 'none';
    return $settings;
}
add_filter( 'media_view_settings', 'theme_gallery_defaults' );

/**
*[只对指定的类型进行搜索]
*@param[type] $query [搜索的参数]
*/
function SearchFilter($query){
    //仅搜索时
    if ($query->is_search && !isset($query->query_vars['post_type'])) {
        //设定指定的文章类型，这里仅搜索文章
        $query->set('post_type','post');
        //指定文章和自定义类型
        //$query->set('post_type', array('post','custom-post-type'));
        //排除指定的文章ID号
        //$query-->set('post__not_in', array(10,11,20,105));
        //搜索指定的类型
        //$query->set('cat','8,15');
        //搜索条件....
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');

function zhizhi_excerpt_length( $length ) {
    return 200;
}
add_filter( 'excerpt_length', 'zhizhi_excerpt_length', 999 );
